//
//  DMAlertView.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/16.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMAlertView.h"

#define mainView_W 228
#define mainView_H 308
@interface DMAlertView ()

//图片
@property (nonatomic, strong) UIImageView *headImage;
//标题
@property (nonatomic, strong) UILabel *titleLb;

@end

@implementation DMAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self= [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        [self makeUI];
    }
    return self;
}

-(void)makeUI {
    self.hidden = YES;
    
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.4;
    UITapGestureRecognizer * disTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideClick)];
    [_bgView addGestureRecognizer:disTap];
    [self addSubview:_bgView];
    
    _mainView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-mainView_W)/2, -400, mainView_W, mainView_H)];
    _mainView.backgroundColor = DMWhiteColor;
    _mainView.clipsToBounds = YES;
    _mainView.layer.cornerRadius = 20;
    //设置阴影
    _mainView.layer.shadowColor = [UIColor blackColor].CGColor;//阴影颜色
    _mainView.layer.shadowOpacity = 0.9;//阴影透明度，默认0
    _mainView.layer.shadowRadius = 8.0;//阴影半径，默认3
    _mainView.layer.shadowOffset = CGSizeMake(0, 3);//阴影偏移，默认(0, -3)
    [self addSubview:_mainView];
    
    _headImage = [UIImageView new];
    _headImage.image = [UIImage imageNamed:@"querenshoudao"];
    [_mainView addSubview:_headImage];
    [_headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainView).offset(18);
        make.left.equalTo(_mainView).offset(12);
        make.right.equalTo(_mainView).offset(-12);
        make.width.height.equalTo(@173);
    }];
    
    _titleLb = [UILabel new];
    _titleLb.text = @"您确认收到宝贝了吗";
    _titleLb.textColor = DMMainColor;
    _titleLb.font = DMFontSize(14);
    _titleLb.textAlignment = NSTextAlignmentCenter;
    [_mainView addSubview:_titleLb];
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_headImage).offset(33);
        make.left.right.equalTo(_mainView);
        make.height.equalTo(@20);
    }];
    
    UIButton *cancelBt = [UIButton new];
    [cancelBt setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBt setTitleColor:DMMainColor forState:UIControlStateNormal];
    cancelBt.titleLabel.font = DMFontSize(14);
    cancelBt.layer.cornerRadius = 15;
    cancelBt.layer.borderWidth = 1;
    cancelBt.layer.borderColor = DMWhiteColor.CGColor;
    //设置阴影
    cancelBt.layer.shadowColor = RGBCOLOR(198, 227, 255).CGColor;//阴影颜色
    cancelBt.layer.shadowOpacity = 1.0;//阴影透明度，默认0
    cancelBt.layer.shadowRadius = 3.0;//阴影半径，默认3
    cancelBt.layer.shadowOffset = CGSizeMake(0, 2);//阴影偏移，默认(0, -3)
    [cancelBt addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [_mainView addSubview:cancelBt];
    [cancelBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_mainView).offset(-21);
        make.left.equalTo(_mainView).offset(23);
        make.width.equalTo(@76);
        make.height.equalTo(@30);
    }];
    
    UIButton *confirmBt = [UIButton new];
    [confirmBt setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBt setTitleColor:DMMainColor forState:UIControlStateNormal];
//    confirmBt.backgroundColor = ;
    confirmBt.titleLabel.font = DMFontSize(14);
    confirmBt.layer.cornerRadius = 15;
    confirmBt.layer.borderWidth = 1;
    confirmBt.layer.borderColor = DMWhiteColor.CGColor;
    //设置阴影
    confirmBt.layer.shadowColor = RGBCOLOR(198, 227, 255).CGColor;//阴影颜色
    confirmBt.layer.shadowOpacity = 1.0;//阴影透明度，默认0
    confirmBt.layer.shadowRadius = 3.0;//阴影半径，默认3
    confirmBt.layer.shadowOffset = CGSizeMake(0, 2);//阴影偏移，默认(0, -3)
    [confirmBt addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [_mainView addSubview:confirmBt];
    [confirmBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_mainView).offset(-21);
        make.right.equalTo(_mainView).offset(-23);
        make.width.equalTo(@76);
        make.height.equalTo(@30);
    }];
}

//取消
-(void)cancelClick {
    [self hideClick];
}

// 确认
-(void)confirmClick {
    if (self.sureBlock) {
        self.sureBlock();
    }
}


-(void)showAlertViewWithTitle:(NSString *)title status:(NSInteger)num {
    _titleLb.text = title;
    switch (num) {
        case 1:
            _headImage.image = [UIImage imageNamed:@"querenshoudao"];
            break;
        case 2:
            _headImage.image = [UIImage imageNamed:@"quxiaodingdan"];
            break;
        default:
            break;
    }
    [self showView];
}

-(void)showView {
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        _mainView.frame = CGRectMake((ScreenWidth-mainView_W)/2, 168, mainView_W, mainView_H);
    }];
}

-(void)hideClick {
    [UIView animateWithDuration:0.3 animations:^{
        _mainView.frame = CGRectMake((ScreenWidth-mainView_W)/2, ScreenHeight, mainView_W, mainView_H);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        _mainView.frame = CGRectMake((ScreenWidth-mainView_W)/2, -400, mainView_W, mainView_H);
    }];
}

@end
